﻿using CuteSprite.Eme.Data.Settlement;
using CuteSprite.Eme.UI.Utils;
using HmFramework.Data;
using HmFramework.UI.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CuteSprite.Eme.UI.Settlement
{
  public partial class FrmBillingPriceDetailAdmin : EmeCommonAdminForm
  {
    #region -- 属性--

    private Int32 _BillingPriceID;

    public Int32 BillingPriceID
    {
      get { return _BillingPriceID; }
      set { _BillingPriceID = value; }
    }

    #endregion

    #region -- 构造 --
    /// <summary>默认构造函数</summary>
    public FrmBillingPriceDetailAdmin(Int32 billingpiceid)
		{
			EntityDataSource = BsdEntityHelper.ModelSettlement;
			EntityDataTable = BsdEntityHelper.SSTableBillingPriceDetail;
      BillingPriceID = billingpiceid;

			InitForm();
		}
    #endregion

    #region -- 重载自定义查询 --

    /// <summary>重载自定义查询</summary>
    /// <returns></returns>
    protected override WhereExpression MakeCurrentWhereExp()
    {
      if (BillingPriceID > 0)
      {
        return CurrentEntityFactory.MakeEqual(BillingPriceDetail._.BillingPriceID, BillingPriceID);
      }
      else
      {
        return base.MakeCurrentWhereExp();
      }
    }

    #endregion 

    #region -- 新增计费价格方案 --

    /// <summary>新增计费价格方案</summary>
    protected override void OnAddButtonClick()
    {
      // 创建新的实体对象
      IEntity entity = CurrentEntityOperate.Create(true);
      // 排序字段赋值，默认取分页控件的总记录数+1
      entity[EntityHelper.FieldSort] = PageBar.RecordCount + 1;

      var frm = new FrmBillingPriceDetail(entity, BillingPriceID);
      var result = frm.ShowDialog(this);
      if (result == DialogResult.OK || result == DialogResult.Yes) { AddEntitySuccess(frm.CurrentEntity); }
      frm.Close();
    }

    #endregion

    #region -- 编辑计费价格方案 --

    /// <summary>编辑计费方案</summary>
    protected override void OnEditButtonClick()
    {
      if (FindSelectRowCount() < 1)
      {
        DialogHelper.ShowWarning(null, "请选择需要编辑的{0}！".FormatWith(CurrentModelTable.DisplayName));
      }
      else
      {
        var loadEntity = ServerManagement.LoadEntityByKey(CurrentEntityOperate, CurrentModel.Name, CurrentModelTable.Name, SelectedEntity[EntityHelper.FieldPrimaryID]);

        // 创建窗体对象，优先根据编辑模板创建
        var form = new FrmBillingPriceDetail(loadEntity, BillingPriceID);
        // 如果没有编辑模板，根据新建模板创建
        var result = form.ShowDialog(this);
        if (result == DialogResult.OK)
        {
          SelectedEntity.CopyFrom(form.CurrentEntity);
          // 编辑成功
          EditEntitySuccess(form.CurrentEntity);
        }
        else if (result == DialogResult.Yes)
        {
          // 另存成功
          AddEntitySuccess(form.CurrentEntity);
        }
      }
    }

    #endregion

    #region -- 浏览计费价格方案 --

    /// <summary>点击浏览按钮触发</summary>
    protected override void OnViewButtonClick()
    {
      if (FindSelectRowCount() < 1)
      {
        DialogHelper.ShowWarning(null, "请选择需要浏览的{0}！".FormatWith(CurrentModelTable.DisplayName));
      }
      else
      {
        var selEntity = SelectedEntity;

        var loadEntity = ServerManagement.LoadEntityByKey(CurrentEntityOperate, CurrentModel.Name, CurrentModelTable.Name, selEntity[EntityHelper.FieldPrimaryID]);

        // 创建窗体对象，优先根据查看模板创建
        var form = new FrmBillingPriceDetail(loadEntity,true, BillingPriceID);
        var result = form.ShowDialog(this);
      }
    }

    #endregion 
  }
}
